#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2022/1/18 15:24
    Desc  :
--------------------------------------
"""
from django_http_log.serializers import DynamicFieldsModelSerializer
from rest_framework import serializers

from person.models import CaseModule


class CaseModuleSerializer(DynamicFieldsModelSerializer):

    class Meta:
        model = CaseModule
        fields = '__all__'


class CaseModuleChildrenSerializer(DynamicFieldsModelSerializer):
    children = serializers.SerializerMethodField()
    parentModuleName = serializers.SerializerMethodField()

    class Meta:
        model = CaseModule
        fields = '__all__'

    def get_parentModuleName(self, obj):
        if obj.parent:
            queryset = self.Meta.model.objects.filter(id = obj.parent, deleted = False)
            return queryset.first().name
        return None
    def get_children(self, obj):

        if obj:
            queryset = self.Meta.model.objects.filter(parent = obj.id, deleted = False)

            return CaseModuleChildrenSerializer(queryset, many = True, read_only = True, exclude = ['deleted', 'deleteTime']).data
        return []


class CaseModuleArraySerializer(DynamicFieldsModelSerializer):
    children = serializers.SerializerMethodField()
    parentModuleName = serializers.SerializerMethodField()

    class Meta:
        model = CaseModule
        fields = '__all__'
        deepth = 5

    def get_parentModuleName(self, obj):
        if obj.parent:
            queryset = self.Meta.model.objects.filter(id = obj.parent, deleted = False)
            return queryset.first().name
        return None

    def get_children(self, obj):

        if obj:
            queryset = self.Meta.model.objects.filter(parent = obj.id, deleted = False)

            return CaseModuleArraySerializer(queryset, many = True, read_only = True, exclude = ['deleted', 'deleteTime']).data
        return []
